const app = getApp()
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const domain = app.globalData.domain;
const apimodule = app.globalData.domain+'/Api/';
const web_url = new Array;
web_url['check_login'] = apimodule+'Wechat/check_login';
web_url['recharge_count'] = apimodule+'/combase/setMeal';
web_url['index'] = apimodule+'Combase/getrefreshdata';
web_url['get_code'] = apimodule+'Sms/send';
web_url['mobile_login'] = apimodule+'User/mobilelogin';
web_url['get_code_check'] = apimodule+'User/sendresetsms';//查询密码
web_url['check_password'] = apimodule+'User/resetipwd';//重置查询密码
web_url['checkipwd'] = apimodule+'User/checkipwd';//查询密码判断
web_url['query'] = apimodule+'Resource/query';
web_url['default_list'] = apimodule+'Index/default_list';
web_url['public_list'] = apimodule+'Index/public_list';
web_url['public_view'] = apimodule+'Index/public_view';
web_url['recharge_pay'] = apimodule+'Payment/addorder2';
web_url['recharge_pay2'] = apimodule+'Payment/addorder3';
web_url['gettimes'] = apimodule+'User/getimes';
web_url['token_check'] = apimodule+'Token/check';
web_url['get_openid'] = apimodule+'Wechat/check_login';
web_url['public_list_user'] = apimodule+'Index/public_list_user';
web_url['public_view_manage'] = apimodule+'Index/public_view_manage';
web_url['get_headimg'] = apimodule+'Combase/geticonlist';
web_url['personinfor'] = apimodule+'User/personinfor';
web_url['my'] = apimodule+'User/my';
web_url['refresh'] = apimodule+'Token/refresh';//刷新Token
web_url['collect'] = apimodule+'Index/collect';
web_url['public_my'] = apimodule+'Index/public_my';
web_url['my_article'] = apimodule+'Index/public_my_article';
web_url['zhuxiao'] = apimodule+'User/cancelaccount';//注销账号

module.exports = {
  formatTime:formatTime,
  web_url:web_url,
  msg:msg,
  msg2:msg2,
  login_id:login_id,
  bindinput:bindinput,
  check_login:check_login,
  login_out:login_out,
  default_value:default_value,
  get_field_value:get_field_value
}
/**
 * 消息提示框
 * @param {*} msg 
 * @param {*} time 
 * @param {*} icon  success error loading none
 * image自定义本地图标
 */
function msg(msg = '',icon = 'none',time = 2000){
  wx.showToast({
    title:msg,
    icon: icon,
    duration: time,
    mask:false
  })
}
/**
 * 模态对话框
 */
function msg2(title='',content=''){
  wx.showModal({
    title: title,
    content: content,
    success (res) {
      if (res.confirm) {
        console.log('用户点击确定')
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }
  })
}

function msg3(){
  wx.showActionSheet({
    itemList: ['列1','列2','列3'],//显示的列表项
    success: function (res) {//res.tapIndex点击的列表项
       console.log("点击了列表项：" + that[res.tapIndex])
    },
    fail: function (res) { },
    complete:function(res){ }
  })
}

  // console.log(typeof(""))             // string
  // console.log(typeof(null))           // object
  // console.log(typeof(NaN))            // number
  // console.log(typeof("abc"))          // string
  // console.log(typeof("123"))          // string
  // console.log(typeof(123))            // number
  // console.log(typeof(new Date()))     // object
  // console.log(typeof({}))             // object
  // console.log(typeof({name:'Lily'}))  // object
  // console.log(typeof(['a', 'b']))     // object
  // console.log(typeof(abc))            // undefined
  // console.log(typeof(true))           // boolean
  // console.log(typeof(false))          // boolean
  // console.log(typeof(class c{}))      // function
  // console.log(typeof(function(){}))   // function
/**
 * 给变量一个默认的值
 * @param {*} value 
 */
function default_value(value){
  if(value==null || typeof(value)=="undefined" || value.trim()=="") {
    return ''
    }else{
      return value
    }
}
/**
 * 通用设置表单的值
 * @param {*} e 页面对象
 * @param {*} that 
 * @param {*} form_name  表单名称
 */
function bindinput(e,that,form_name='form_data'){
  //console.log(form_name)
  var value = e.detail.value;
  var field = e.target.dataset.field;
  var validate = this.default_value(e.target.dataset.validate);
  console.log(validate)
  if(validate == 'confirm' && value.length>=6){
    console.log(11)
    if(that.data[form_name].password != value){
      this.msg('两次密码不一致');
    }
  }

  var empty_object ={}
  empty_object[form_name] = {}; 
  if(!that.data.hasOwnProperty(form_name)){ //没有设置的时候自动设置一个
    that.setData(empty_object)
  }
  var index = that.data[form_name].index
 
  if(e.detail.value){
    that.data[form_name][e.currentTarget.dataset.field] = e.detail.value
  }else{
    that.data[form_name][e.currentTarget.dataset.field] = e.detail.value
    for (var index in that.data[form_name]) {
      that.data[form_name].index;
    }
  }

  
  that.setData({
    [form_name]: that.data[form_name]
  })


}
/**
 * 检测登录
 */
function check_login(){
  var  token = wx.getStorageSync('token')
  //var token = getApp().globalData.token;
  if(!token){
    //msg('请登录')
    setTimeout(function(){
      wx.reLaunch({
        url: '/pages/denglu/denglu',
      })
    },500);
    return
  }
  
  wx.request({
    url: this.web_url.token_check,
    data: {token:wx.getStorageSync('token')},
    method: 'POST',
    header: {
      'content-type':  'application/x-www-form-urlencoded',
    },
    dataType: 'json',
    success: function (res) {
      console.log(res)
      if(res.code == 401){
        wx.request({
          url: this.web_url.refresh,
          data: {token:wx.getStorageSync('token')},
          method: 'POST',
          header: {
            'content-type':  'application/x-www-form-urlencoded',
          },
          dataType: 'json',
          success: function (res) {
            console.log(res)
            
          }
      
       })
      }
      var date= new Date();

    }

 })

}

function login_id(){
  return wx.getStorageSync('token')
}
/**
 * 退出登录
 */
function login_out(){
  wx.setStorageSync('token','')
  wx.setStorageSync('userinfo','')
  wx.clearStorageSync()
  wx.reLaunch({
    url: '/pages/denglu/denglu',
  })
  return
}
//取出第一条数据的值
function get_field_value(data,that,j=0,field=''){
  var len = data.length
  for(var i=0;i<=len;i++){
    if(i == j){
      if(field){
        that.setData({year:data[i][field]})
        return data[i][field]
      }else{
        that.setData({year:data[i]})
        return data[i]
      }
    }
    $i++
  }
}
